package com.example.demo.controller;

import com.example.demo.VO.Result;
import com.example.demo.entity.Course;
import com.example.demo.entity.MyCourse;
import com.example.demo.service.CourseService;
import com.example.demo.service.MyCourseService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * (Course)表控制层
 *
 * @author hjl
 * @since 2020-11-30 19:33:01
 */
@RestController
@RequestMapping("course")
public class CourseController {
    /**
     * 服务对象
     */
    @Resource
    private CourseService courseService;

    @Resource
    private MyCourseService myCourseService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public Course selectOne(Integer id) {
        return this.courseService.queryById(id);
    }

    @GetMapping("getAllCourse")
    public Result getAllCourse() {
        return Result.success(courseService.queryByExample(new Course()));
    }

    @GetMapping("getUserCourse")
    public Result getUserCourse(Integer userID) {
        Course course = new Course();
        course.setUserId(userID);
        List<Course> courseList =courseService.queryByExample(course);
        if(courseList.size()==0){
            return Result.failure("没有课程");
        }else{
            return Result.success(courseList);
        }
    }


    @GetMapping("getUserNotJoinedCourse")
    public Result getUserNotJoinedCourse(Integer userID) {
        Course course = new Course();
        course.setUserId(userID);
        List<Course> courseList = courseService.queryByExample(new Course());
        List<Course> courseCreateList =courseService.queryByExample(course);
        MyCourse myCourse=new MyCourse();
        myCourse.setUserId(userID);
        List<MyCourse> myCourseList=myCourseService.queryByExample(myCourse);
        List<Course> courseJoinList=new ArrayList<>();
        for(int i=0;i<myCourseList.size();i++)
        {
            courseJoinList.add(courseService.queryById(myCourseList.get(i).getCourseId()));
        }

        courseList.removeAll(courseJoinList);
        courseList.removeAll(courseCreateList);
        if(courseList.size()==0){
            return Result.failure("没有课程了");
        }else{
            return Result.success(courseList);
        }
    }
    @PostMapping("addCourse")
    public Result addCourse(Course course){
        courseService.insert(course);
        return Result.success(courseService.queryByExample(course));
    }
    @DeleteMapping("delCourse")
    public Result delCourse(int CourseID){
        return Result.success(courseService.deleteById(CourseID));
    }


}